//
//  StartFourViewController.m
//  yinsiwenzhen
//
//  Created by Yangfan on 14/12/24.
//  Copyright (c) 2014年 4gread. All rights reserved.
//

#import "StartFourViewController.h"
#import "StartFiveViewController.h"
@interface StartFourViewController ()

@end

@implementation StartFourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (isiphone6plus) {
        _destributionWord.font = [UIFont systemFontOfSize:24];
        _navigationTitle.font = [UIFont systemFontOfSize:20];
    } else {
        _destributionWord.font = [UIFont systemFontOfSize:20];
        _navigationTitle.font = [UIFont systemFontOfSize:19];
    }
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"第五步";
    self.navigationItem.backBarButtonItem = backItem;
    
    _yesButton.adjustsImageWhenHighlighted = NO;
    _noButton.adjustsImageWhenHighlighted = NO;
    
    _imageDrug = [[UIImageView alloc] init];
    _imageYaoWuWord = [[UIImageView alloc] init];
    [_imageDrug setUserInteractionEnabled:YES];
    if (isiphone6plus) {
        [_imageDrug setFrame:CGRectMake(83, 267, Kwidth - 83 * 2, (Kwidth - 83 * 2))];
//        [_imageYaoWuWord setFrame:CGRectMake(83, 267 + (Kwidth - 83 * 2), Kwidth - 83 * 2, (Kwidth - 83 * 2) * 35.0 / 249.0)];
    } else {
        if (isiphone4) {
            [_imageDrug setFrame:CGRectMake(60, 222, Kwidth - 60 * 2, (Kwidth - 60 * 2))];
//            [_imageYaoWuWord setFrame:CGRectMake(60, 222 + (Kwidth - 60 * 2), Kwidth - 60 * 2, (Kwidth - 60 * 2) * 35.0 / 249.0)];
        } else {
            [_imageDrug setFrame:CGRectMake(60, 252 , Kwidth - 60 * 2, (Kwidth - 60 * 2))];
//            [_imageYaoWuWord setFrame:CGRectMake(60, 252 + (Kwidth - 60 * 2), Kwidth - 60 * 2, (Kwidth - 60 * 2) * 35.0 / 249.0)];
        }
    }
    [self.view addSubview:_imageDrug];
    [self.view addSubview:_imageYaoWuWord];
    

    
    _addButton = [[UIButton alloc] init];
    [_addButton setFrame:CGRectMake(0, 0, _imageDrug.frame.size.width, _imageDrug.frame.size.width)];
    [_addButton setBackgroundColor:[UIColor clearColor]];
    [_addButton addTarget:self action:@selector(actionAddImage:) forControlEvents:UIControlEventTouchUpInside];
    [_imageDrug addSubview:_addButton];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (_listView.donotDrug) {
        [_yesButton setSelected:NO];
        [_noButton setSelected:YES];
        [_yesButton setUserInteractionEnabled:YES];
        [_noButton setUserInteractionEnabled:NO];
        [_imageDrug setImage:[UIImage imageNamed:@"bigCameraNo"]];
        [_imageYaoWuWord setImage:[UIImage imageNamed:@"bigCameraNoWord"]];
    } else {
        [_yesButton setSelected:YES];
        [_noButton setSelected:NO];
        [_yesButton setUserInteractionEnabled:NO];
        [_noButton setUserInteractionEnabled:YES];
        if (_listView.imageDrug == nil) {
            _imageDrug.image = [UIImage imageNamed:@"bigCamera"];
            [_imageYaoWuWord setImage:[UIImage imageNamed:@"bigCameraWord"]];
        } else {
            _imageDrug.image = _listView.imageDrug;
            [_imageYaoWuWord setImage:[UIImage imageNamed:@"bigCameraWord"]];
        }
    }
}
-(void)pushButtonClick:(UIButton *)sender{
    StartFiveViewController *five = [[StartFiveViewController alloc] init];
    [self.navigationController pushViewController:five animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)actionYesButton:(UIButton *)sender {
    if (!sender.selected) {
        [sender setSelected:!sender.isSelected];
        [_noButton setSelected:!_noButton.isSelected];
        [sender setUserInteractionEnabled:NO];
        [_noButton setUserInteractionEnabled:YES];
    }
    
    if (_listView.imageDrug == nil) {
        _imageDrug.image = [UIImage imageNamed:@"bigCamera"];
    } else {
        _imageDrug.image = _listView.imageDrug;
    }
        _imageYaoWuWord.image = [UIImage imageNamed:@"bigCameraWord"];
    [_addButton setUserInteractionEnabled:YES];
    
    _listView.donotDrug = FALSE;
//    [sender setSelected:YES];
//    [_noButton setSelected:NO];
//    
//    [_addButton setBackgroundColor:[UIColor clearColor]];
//    [_addButton setUserInteractionEnabled:YES];
}
- (IBAction)actionNoButton:(UIButton *)sender {
    if (!sender.selected) {
        [sender setSelected:!sender.isSelected];
        [_yesButton setSelected:!_yesButton.isSelected];
        [sender setUserInteractionEnabled:NO];
        [_yesButton setUserInteractionEnabled:YES];
    }
    _imageDrug.image = [UIImage imageNamed:@"bigCameraNo"];
    _imageYaoWuWord.image = [UIImage imageNamed:@"bigCameraNoWord"];
    [_addButton setUserInteractionEnabled:NO];
    _listView.donotDrug = TRUE;
//    [sender setSelected:YES];
//    [_yesButton setSelected:NO];
//    
//    [_addButton setBackgroundColor:[UIColor grayColor]];
//    [_addButton setUserInteractionEnabled:NO];
}

- (void)actionAddImage:(UIButton *)sender {
    UIActionSheet *sheet;
    // 判断是否支持相机
    if(isCamera)
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    }
    else {
        sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    [sheet showInView:self.view];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 2) {
        return;
    }
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    
    NSUInteger sourceType = 0;
    if (isCamera) {
        switch (buttonIndex) {
            case 0:
            {
                // 相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePickerController.allowsEditing = NO;
                imagePickerController.sourceType = sourceType;
                [imagePickerController setShowsCameraControls:YES];
                //让相机显示全屏
                CGSize screenBounds = [UIScreen mainScreen].bounds.size;
                CGFloat scale = 4.0f/3.0f;
                CGFloat camViewHeight = screenBounds.height / scale;
                imagePickerController.cameraViewTransform = CGAffineTransformMakeTranslation(0, Kheight/2 - 45 - camViewHeight/2);
                imagePickerController.cameraViewTransform = CGAffineTransformScale(imagePickerController.cameraViewTransform, scale + 0.1, scale + 0.1);
            }
                break;
            case 1:
            {
                // 相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
                break;
            default:
                break;
        }
    } else {
        switch (buttonIndex) {
            case 0:
            {
                // 相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
                break;
            default:
                return;
                break;
        }
    }
    
    [self presentViewController:imagePickerController animated:YES completion:^{
    }];
}

#pragma markUIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _imageDrug.image = image;
    
    _listView.imageDrug = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"toFiveAction"]) {
        UIViewController *fiveViewController = segue.destinationViewController;
        if ([fiveViewController respondsToSelector:@selector(setListView:)]) {
            if (![_yesButton isSelected]) {
                [_listView setDonotDrug:TRUE];
            } else {
                [_listView setDonotDrug:FALSE];
                _listView.imageDrug = _imageDrug.image;
            }
            [fiveViewController setValue:_listView forKey:@"listView"];
        }
    }
}
@end
